package com.landeb.shop.huo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.landeb.shop.R;
import com.landeb.shop.huo.bean.BeanFragProducts;
import com.landeb.shop.huo.bean.BeanProductFragLeftLv;

import java.util.List;

/**
 * Created by blade on 2016/10/21.
 */

public class FragProductsLVAdapter extends BaseAdapter {

    //    private List<ItemFragProductsListView> mList;
    private Context mContext;
    private LayoutInflater mInflater;
    private List<BeanProductFragLeftLv.DataBean> mProductsList;

    private int mSelectPosition;

    public int getSelectPosition() {
        return mSelectPosition;
    }

    public void setSelectPosition(int selectPosition) {
        mSelectPosition = selectPosition;
    }

    public FragProductsLVAdapter(Context context, List<BeanProductFragLeftLv.DataBean> productsList) {
        mContext = context;
        mProductsList = productsList;
        mInflater = LayoutInflater.from(context);
    }

    public void changeSelected(int position) { //刷新方法
        if (position != mSelectPosition) {
            mSelectPosition = position;
            notifyDataSetChanged();
        }
    }

    @Override
    public int getCount() {
        return mProductsList.size();
    }

    @Override
    public Object getItem(int position) {
        return mProductsList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_frag_products_listview, parent, false);

            holder = new ViewHolder();

            holder.mTitle = (TextView) convertView.findViewById(R.id.tv_item_fragProducts_listView);
            holder.mIndicator = (TextView) convertView.findViewById(R.id.tv_indicator_fragProducts_listView);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        String title = mProductsList.get(position).getCatename();
        holder.mTitle.setText(title);


        if (mSelectPosition == position) {
            holder.mTitle.setBackgroundResource(R.color.white);  //选中项背景
            holder.mTitle.setTextColor(mContext.getResources().getColor(R.color.colorPrimary));
            holder.mIndicator.setBackgroundResource(R.color.primary);
        } else {
            holder.mTitle.setBackgroundResource(R.color.background);  //其他项背景
            holder.mTitle.setTextColor(mContext.getResources().getColor(R.color.test2));
            holder.mIndicator.setBackgroundResource(R.color.background);

        }

        return convertView;
    }

    private class ViewHolder {
        private TextView mTitle; //文字
        private TextView mIndicator;
    }

}
